﻿using System;
using Vowei.Core.Models;
using System.Collections.Generic;
using System.Linq;
using Vowei.Data.Helpers;
using Vowei.Data.Models;
using Vowei.Data;

namespace Vowei.Web.Models
{
    public class MilestoneProgress : IContainer<MilestoneProgress>
    {
        public MilestoneProgress()
        {
            Children = new List<MilestoneProgress>();
        }

        public Milestone Milestone { get; set; }

        public int TicketCount { get { return ActiveTicketCount + ClosedTicketCount; } }

        public int ActiveTicketCount { get; set; }

        public int ClosedTicketCount { get; set; }

        /// <summary>
        /// 返回剩余的工作量，以时间，按小时为单位（可以包括小数，例如2.5小时）计数，
        /// </summary>
        public float LeftEffort
        {
            get
            {
                return 0.0f;
            }
        }

        /// <summary>
        /// 返回已经完成的工作量，以时间，按小时为单位（可以包括小数，例如2.5小时）计数，
        /// </summary>
        public float FinishedEffort
        {
            get
            {
                return 0.0f;
            }
        }

        #region IContainer<MilestoneProgress> Members
        IQueryable ITableItemContainer.Children
        {
            get
            {
                return Children.AsQueryable();
            }
        }

        public List<MilestoneProgress> Children
        {
            get;
            private set;
        }

        #endregion

        // TODO: 将这个函数挪到数据层去，除了在vowei.data项目里可以用到Linq to Entity以外，其他地方都不应该用到它
        public static MilestoneProgress CreateFrom(Project project, IVoweiContext context)
        {
            var ms = project.AllMilestones(context).OrderBy(m => m.StartDate);

            // 将项目本身加入进去
            var msList = ms.ToList();
            msList.Add(project);

            var msQuery = from m in msList
                          select new
                          {
                              Milestone = m,
                              Tickets = m.AllTickets(context)
                          };

            var milestones = msQuery.ToList();

            var progresses = new List<MilestoneProgress>(milestones.Count());
            for (int i = 0; i < milestones.Count(); ++i)
            {
                var m = milestones[i];
                var progress = new MilestoneProgress()
                {
                    Milestone = m.Milestone,
                    ActiveTicketCount = m.Tickets.Count(t => !t.ClosedDate.HasValue),
                    ClosedTicketCount = m.Tickets.Count(t => t.ClosedDate.HasValue)
                };

                progresses.Add(progress);
            }

            var result = new MilestoneProgress() { Milestone = project };

            for (int i = 0; i < progresses.Count; ++i)
            {
                if (project.Children.Any(m => m == progresses[i].Milestone))
                    result.Children.Add(progresses[i]);
                if (project == progresses[i].Milestone)
                {
                    result.ActiveTicketCount = progresses[i].ActiveTicketCount;
                    result.ClosedTicketCount = progresses[i].ClosedTicketCount;
                }
            }

            return result;
        }
    }
}